package org.song.filter.certification;

import org.song.exception.result.AjaxResult;
import org.song.util.JacksonUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 身份验证失败处理器
 */
@Component( value = "myAuthenticationEntryPoint")
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {


    /**
     * 身份认证失败处理方法
     * @param request
     * @param response
     * @param e
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        // 响应身份验证不足的提示信息给前端
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        // 响应数据给前端
        response.getWriter().write(JacksonUtil.getJsonString( AjaxResult.insufficientIdentity( "访问此资源需要完全身份验证： "+e.getMessage() ) ));
    }

}
